package cn.wolfcode.web.Interceptor;

import cn.wolfcode.util.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//         访问静态资源的时候对应的父类是这个  DefaultServletHttpRequestHandler
//        访问动态资源对应的父类是这个         HandlerMethod
//        System.out.println("==>"+handler.getClass());
        if (handler instanceof HandlerMethod){
            Object emp = UserContext.getUser();
            if (emp==null){
                response.sendRedirect("/login.html");
                return false;
            }
            
        }
        return true;
    }
}
